package com.example.notificationtest

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val channel = NotificationChannel("normal","Normal",NotificationManager.IMPORTANCE_DEFAULT)
        manager.createNotificationChannel(channel)



        btnNotification.setOnClickListener {
            val intent = Intent(this,NotificationActivity::class.java)
            val pi = PendingIntent.getActivities(this,0, arrayOf(intent),0)
            val notification = NotificationCompat.Builder(this,"normal")
                .setContentTitle("标题")
                .setContentText("内容...")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.xnylh))
                .setContentIntent(pi)
                    //自动取消通知
                .setAutoCancel(true)
                .build()
            manager.notify(1,notification)
        }
    }
}